<?php
    require_once 'files.php';
    require_once 'model/db.php';
    $query = "SELECT * FROM apps";
    $apps = getData($query);
?>
<html>
    <head>
        <title>Nguyễn Quốc Thông</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="stylesheet" type="text/css" href="public/css/style2.css">
    </head>
    <body>
        <div class="container">
            <div class="header">
                <div class="form-search">
                    <h1>Tìm kiếm Fanpages Facebook</h1>
                    <form method="POST">
                        <table>
                            <tr>                
                                <td>Min likes</td>
                                <td><input type="text" id="min_likes" name="min_likes" value="1"></td>
                            </tr>
                            <tr>
                                <td>Max likes</td>
                                <td><input type="text" id="max_likes" name="max_likes" value="10"></td>
                            </tr>            
                            <tr>
                                <td>Apps</td>
                                <td>
                                    <select name="id_app">
                                        <?php foreach ($apps as $app): ?>
                                            <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2"><input type="submit" name="submit" value="Search"></td>
                            </tr>                     
                        </table>
                    </form>
                </div>
                <div class="info">
                    <table>
                        <tr>
                            <td>Họ và tên:</td>
                            <td><b>Nguyễn Quốc Thông</b></td>
                        </tr>
                        <tr>
                            <td>MSSV:1150TD1489</td>
                            <td><b>KTWEB1</b></td>
                        </tr>
                        <tr>
                            <td>Lượt truy cập:</td>
                            <td><b><?php echo mwriteLogs();?></b></td>
                        </tr>
                    </table>
                </div>
            </div>          
        <?php if (isset($_POST['submit'])): ?>
            <?php
            $min_likes = $_POST['min_likes'];
            $max_likes = $_POST['max_likes'];
            $id_app    = $_POST['id_app'];
            $query = "SELECT pages.name,
                             pages.likes,
                             pages.description,
                             apps.name AS app_name                             
                        FROM pages 
                        INNER JOIN apps_pages ON apps_pages.id_page = pages.id
                        INNER JOIN apps       ON apps.id = apps_pages.id_app
                        WHERE (likes <= $max_likes)
                        AND (likes >= $min_likes)                        
                        AND (apps.id LIKE '$id_app')
                        LIMIT 0, 5
                  ";
            $pages = getData($query);
            ?>
            <?php if (!empty($pages)): ?>        
            <div class="results">
                <table border="1">                    
                    <tr>
                        <td> Fanpage name </td>
                        <td> Likes   </td>
                        <td> Description </td>
                    </tr>
                    <?php foreach ($pages as $page): ?>
                        <?php if(!empty($page['name']) && (!empty($page['description']))): ?>
                        <tr>
                            <td><?php echo $page['name'] ?>></td>
                            <td><?php echo $page['likes'] ?></td>
                            <td><?php echo substr($page['description'], 0, 100)?></td>
                        </tr>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </table> 
            </div>
            <?php endif; ?>
        <?php endif; ?>
     </div>
    </body>
</html>
